package com.sport.setting.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sport.model.common.dtos.ResponseResult;
import com.sport.model.common.enums.AppHttpCodeEnum;
import com.sport.model.setting.dtos.SportEventsGamesDto;
import com.sport.model.setting.dtos.SportEventsTempGameDto;
import com.sport.model.setting.pojos.SportEventsGames;
import com.sport.model.setting.bos.SportEventsGamesBo;
import com.sport.model.setting.pojos.SportEventsPreEnroll;
import com.sport.model.user.bos.AccountUserBo;
import com.sport.setting.mapper.SportEventsPreEnrollMapper;
import com.sport.setting.mapper.SportEventsTempGameMapper;
import com.sport.setting.mapper.SportMeetingDefineItemMapper;

import com.sport.utils.common.BaseUtils;
import com.sport.utils.common.ThreadPoolUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Transactional
public class SportMeetingDefineItemRepository extends ServiceImpl<SportMeetingDefineItemMapper, SportEventsGames> {

    @Resource
    private SportMeetingDefineItemMapper sportMeetingDefineItemMapper;

    @Resource
    private SportEventsPreEnrollMapper sportEventsPreEnrollMapper;

    @Resource
    private SportEventsTempGameMapper sportEventsTempGameMapper;

    public ResponseResult defineItem(SportEventsGamesDto sportEventsGamesDto) {
        SportEventsGames sportEventsGames = BaseUtils.copyProperties(sportEventsGamesDto, SportEventsGames.class);
        sportEventsGames.setSchoolTenementId(sportEventsGamesDto.getAccountUserBo().getSchoolTenementId());
        if(sportMeetingDefineItemMapper.defineProject(sportEventsGames) == 1)
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

        return ResponseResult.okResult("操作失败");
    }

    public List<String> getEventsGamesToTemplate() {
        List<SportEventsGamesBo> sportEventsGamesBos = BaseUtils.copyPropertiesList(
                sportMeetingDefineItemMapper.selectList(new QueryWrapper<>())
                , SportEventsGamesBo.class
        );
        List<Object> list = sportEventsPreEnrollMapper.selectObjs(Wrappers.lambdaQuery(SportEventsPreEnroll.class).select(SportEventsPreEnroll::getSportEventsGameId).eq(SportEventsPreEnroll::getSchoolTenementId, AccountUserBo.getUser().getSchoolTenementId()));
        List<Long> idList = list.stream().map(obj -> (Long) obj).collect(Collectors.toList());
        List<String> record = new ArrayList<>();
        for (SportEventsGamesBo sportEventsGamesBo : sportEventsGamesBos) {
            if(idList.contains(sportEventsGamesBo.getId())){
                sportEventsGamesBo.setSportName(sportEventsGamesBo.getSportEventsName()+sportEventsGamesBo.getSportEventsSonType());
                record.add(sportEventsGamesBo.getSportName());
            }
        }
        return record;
    }

    public List<SportEventsGamesBo> getEventsGames(String schoolTenementId) {
        List<SportEventsGamesBo> sportEventsGamesBos = BaseUtils.copyPropertiesList(
                sportMeetingDefineItemMapper.selectList(new QueryWrapper<SportEventsGames>()
                        .in("school_tenement_id", schoolTenementId, 0)
                        .select("id","sport_events_name","sport_events_type","sport_events_son_type"))
                , SportEventsGamesBo.class
        );
        for (SportEventsGamesBo sportEventsGamesBo : sportEventsGamesBos) {
            sportEventsGamesBo.setSportName(sportEventsGamesBo.getSportEventsName()+sportEventsGamesBo.getSportEventsSonType());
        }

        return sportEventsGamesBos;
    }

    public boolean deleteItemProject(List<Long> deleteIds, String schoolTenementId) {
        return sportMeetingDefineItemMapper.delete(new QueryWrapper<SportEventsGames>().in("id", deleteIds).eq("school_tenement_id", schoolTenementId)) >= 1;
    }

    public List<Long> getTempGameIds(String schoolTenementId, Long sportBindingSchool) {
        return sportEventsTempGameMapper.getTempGameForSchool(schoolTenementId, sportBindingSchool);
    }

    public List<SportEventsGamesBo> getTempGameForSchool(String schoolTenementId, Long sportBindingSchool) {
        List<Long> ids = getTempGameIds(schoolTenementId, sportBindingSchool);
        List<SportEventsGamesBo> sportEventsGamesBos = BaseUtils.copyPropertiesList(
                sportMeetingDefineItemMapper.selectList(new QueryWrapper<SportEventsGames>().in("id", ids))
                , SportEventsGamesBo.class
        );
        if (sportEventsGamesBos == null)
            return null;

        for (SportEventsGamesBo sportEventsGamesBo : sportEventsGamesBos) {
            sportEventsGamesBo.setSportName(sportEventsGamesBo.getSportEventsName()+sportEventsGamesBo.getSportEventsSonType());
        }

        return sportEventsGamesBos;
    }

    public int insertTempGame(SportEventsTempGameDto sportEventsTempGameDto) {
        ThreadPoolUtils.putOperationToThread(()->{
            AccountUserBo accountUserBo = sportEventsTempGameDto.getAccountUserBo();
            for (Long single : sportEventsTempGameDto.getIds())
                sportEventsTempGameMapper.insertTemp(single, accountUserBo.getSchoolTenementId(), accountUserBo.getSportId());
        });

        return 1;
    }

    public int addTempGame(SportEventsTempGameDto sportEventsTempGameDto) {
        AccountUserBo accountUserBo = sportEventsTempGameDto.getAccountUserBo();
        for (Long single : sportEventsTempGameDto.getTarget())
            sportEventsTempGameMapper.insertTemp(single, accountUserBo.getSchoolTenementId(), accountUserBo.getSportId());

        return 1;
    }

    public int deleteTempGame(SportEventsTempGameDto sportEventsTempGameDto) {
        AccountUserBo accountUserBo = sportEventsTempGameDto.getAccountUserBo();
        for (Long single : sportEventsTempGameDto.getTarget())
            sportEventsTempGameMapper.deleteTemp(single, accountUserBo.getSchoolTenementId(), accountUserBo.getSportId());

        return 1;
    }
}
